package lelouet.datacenter.thermal.impacts.impactGenerators;

import gridlib.api.Grid;
import gridlib.api.Machine;

import java.rmi.RemoteException;

import lelouet.datacenter.thermal.impacts.ImpactGenerator;
import lelouet.datacenter.thermal.impacts.ImpactMap;

/**
 * applies temperature increase of servers due to their consumption and a
 * temporally and locally constant airflow
 */
public class SelfImpact implements ImpactGenerator {

	double airFlow;

	public SelfImpact(double airFlow) {
		this.airFlow = airFlow;
	}

	/**
	 * the static heat capacity of air at 30°C, in J/(L.°C)<br />
	 * That is, the number of J one L of air requires to have an increase of 1°C
	 */
	public static final double airHeatCapacity = 1.3;

	/**
	 * @return the increase in temperature of air entering the server, in °C
	 * @param power
	 *            the power that is dissipated by the server, in W
	 * @param airflow
	 *            the flow of air entering the server, in L/s
	 */
	public static double temperatureGain(double power, double airflow) {
		return power / airflow / airHeatCapacity;
	}

	@Override
	public void apply(Grid grid, ImpactMap storage) {
		try {
			for (Machine machine : grid.getAllMachines()) {
				double baseImpact = storage.getImpact(machine, machine);
				storage.setImpact(machine, machine, baseImpact
						+ temperatureGain(1.0, airFlow));
			}
		} catch (RemoteException e) {
			e.printStackTrace(System.err);
		}
	}

}
